using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Forest.Cat.Domain.Entities;
using Forest.Core.Data;
using Forest.Core;

namespace Forest.Cat.Domain.Services.JobHunting
{
    public partial class JobAlertService : IJobAlertService
    {
        #region Constants

        #endregion

        #region Fields
        private readonly IRepository<JobAlert> _jobalertRepository;
        #endregion

        #region Ctor
        public JobAlertService(IRepository<JobAlert> jobalertRepository)
        {
            _jobalertRepository = jobalertRepository;
        }
        #endregion

        #region Base Methods
        public virtual void DeleteJobAlert(JobAlert jobalert)
        {
            if (jobalert == null)
                throw new ArgumentNullException("JobAlert");

            _jobalertRepository.Delete(jobalert);
        }

        public JobAlert GetJobAlertById(Guid jobalertId)
        {
            if (jobalertId == Guid.Empty)
                return null;
            return _jobalertRepository.GetById(jobalertId);
        }

        public virtual IPagedList<JobAlert> GetAllJobAlerts(int pageIndex, int pageSize)
        {
            var query = _jobalertRepository.Table;
            query = query.OrderBy(w => w.Id);
            var items = new PagedList<JobAlert>(query, pageIndex, pageSize);
            return items;
        }

        public IQueryable<JobAlert> GetAllJobAlertsByJobseekerId(Guid jobseekerId)
        {
            var query = _jobalertRepository.Table.Where(w=>w.JobseekerId == jobseekerId).OrderByDescending(o=>o.LastUpdated);
            return query;
        }

        public virtual void InsertJobAlert(JobAlert jobalert)
        {
            if (jobalert == null)
                throw new ArgumentNullException("JobAlert");
            _jobalertRepository.Insert(jobalert);
        }

        public virtual void UpdateJobAlert(JobAlert jobalert)
        {
            if (jobalert == null)
                throw new ArgumentNullException("JobAlert");
            _jobalertRepository.Update(jobalert);
        }
        #endregion

        #region Extend Methods

        public PagedList<JobAlert> GetJobAlertPagedList(int pageIndex, int pageSize)
        {
            var query = FindJobAlertList();
            return new PagedList<JobAlert>(query, pageIndex, pageSize);
        }

        public IQueryable<JobAlert> FindJobAlertList()
        {
            return _jobalertRepository.Table;
        }

        #endregion
    }
}
